<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <liujiqi@etrol.com> 
// +----------------------------------------------------------------------

namespace app\basic\controller;

use app\common\controller\Admin;
use app\basic\controller\api\Chemical as ChemicalApi;
use app\admin\controller\ajax\Library as Ajax;

/**
 * 化学品管理
 * @package app\chemical
 */
class Chemical  extends Admin

{
    
    protected $api;
    
	public function _initialize()
	{
		parent::_initialize();
		$this->api = new ChemicalApi();
        $this->ajax = new Ajax();
	}
    /**
     * 首页
     * 
     */
   	public function index() {
   	  if(IS_AJAX){
   	  	$chemicalList = $this->api->chemicalList($this->scope);
   	  	return $chemicalList;
   	  }
      $type=$this->ajax->getChemicalType()->getData();
         
      $this->assign('type',$type);
      return $this->fetch();
 	}

 	/**
 	 * 救援化学品
 	 */
 	public function search($keyword,$typeId) {
 		if(IS_AJAX){
			$chemicalList = $this->api->search(input('keyword'),input('typeId'));
 			return $chemicalList;
 		}
 	}
    /**
     * 添加化学品
     * 
     */
    public function add() {
   		 if(IS_POST){
   		 	$data = input('post.');  
   		 	if(isset($data['is_supervised'])){
    	  		$data['is_supervised'] = 1;
    	  	}else{
    	  		$data['is_supervised'] = 0;
    	  	}
    	  	if(isset($data['is_major'])){
    	  		$data['is_major'] = 1;
    	  	}else{
    	  		$data['is_major'] = 0;
    	  	}

            return $this->api->add($data);
   		 }
   		 //企业列表
   		 $enterpriseList = model('enterprise')->getSelectList($this->scope);
   		 $this->assign('enterpriseList',$enterpriseList);
   		 
         $type=$this->ajax->getChemicalType()->getData();
         
         $this->assign('type',$type);
          
         return $this->fetch();    
     }
 
    /**
     * 修改化学品
     * 
     */
    public function edit($chemicalId = null) {
      
    	  if(IS_POST){
    	  	$data = input('post.');
    	  	if(isset($data['is_supervised'])){
    	  		$data['is_supervised'] = 1;
    	  	}else{
    	  		$data['is_supervised'] = 0;
    	  	}
    	  	if(isset($data['is_major'])){
    	  		$data['is_major'] = 1;
    	  	}else{
    	  		$data['is_major'] = 0;
    	  	}
    	  	return $this->api->edit($data,$chemicalId);
    	  }
    	  //企业列表
    	  $enterpriseList = model('enterprise')->getSelectList($this->scope);
    	  $this->assign('enterpriseList',$enterpriseList);
    	  //化学品信息
     	  $chemicalList = $this->api->chemicalList($this->scope,$chemicalId)->getData();
     	  $this->assign('info',$chemicalList['data'][0]);
     	  //化学品类型
          $type = $this->ajax->getChemicalType()->getData();
          $this->assign('type',$type);
     	
         return $this->fetch();      
    }
    
    /**
     * 删除化学品
     */
    public function del($chemicalId = null) {

    	if(IS_AJAX){
    		return $this->api->del($chemicalId);
    	}
    }
  }



